package com.sx.shop.controller;


import com.sx.shop.entity.Store;
import com.sx.shop.entity.User;
import com.sx.shop.service.StoreService;
import com.sx.shop.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/store")
public class StoreController {
    // DI 依赖注入
    @Autowired
    private StoreService storeService;
    /* 打开用户主页面*/

    //@RequestMapping(value = "/index" method="get")
    //指定请求方式
    @GetMapping("/index")
    public String openPage() {
        //这是一个html页面
        return "store";
    }


    /* 获取用户列表数据 */
    @GetMapping(value = "/list")
    @ResponseBody//只返回数据不找页面
    public ResultVo selectStore(String keyword , Integer page , Integer limit){
        //Ioc 控制反转
        //UserService service=new UserService();
        User user=(User) SecurityUtils.getSubject().getPrincipal();
        return storeService.selectStore(keyword,page, limit,user.getId());
    }



    @ResponseBody
    @PostMapping(value="/save")
    public void saveStore(Store store) {
        storeService.doSave(store);
    }


    @ResponseBody
    @PostMapping(value="/del")
    public void delStore(@RequestParam(value = "id"/* ,required = false */) Integer storeid) {
        storeService.doDel(storeid);
    }


    @ResponseBody
    @GetMapping(value = "/{id}")
    public Store selectStoreById(@PathVariable("id") Integer id) {

        return 	storeService.selectStoreById(id);
    }

    @ResponseBody
    @GetMapping(value = "/shopid")
    public List<Store> selectStoreByShopid(int shopid) {

        return 	storeService.selectStoreByShopid(shopid);
    }

    //                      通过createby_id查询store
    @ResponseBody
    @GetMapping(value = "/cis")
    public List<Store> selectStoreByCreatebyid() {
        User user=(User) SecurityUtils.getSubject().getPrincipal();
        return 	storeService.selectStoreByCreatebyid(user.getId());
    }

    @GetMapping(value = "/storelist")
    @ResponseBody//只返回数据不找页面
    public ResultVo selectStoreByShopid(String keyword , Integer page , Integer limit,int shopid){
        //Ioc 控制反转
        //UserService service=new UserService();
        return storeService.selectStoreByShopid(keyword,page, limit,shopid);
    }
}
